VERSION 5.00
Begin VB.Form frmODBCTrans 
   Caption         =   "ODBCProcess"
   ClientHeight    =   3135
   ClientLeft      =   60
   ClientTop       =   405
   ClientWidth     =   4680
   LinkTopic       =   "Form1"
   ScaleHeight     =   3135
   ScaleWidth      =   4680
   StartUpPosition =   3  'Windows Default
   Begin VB.TextBox textMsg 
      Height          =   2295
      Left            =   240
      Locked          =   -1  'True
      MultiLine       =   -1  'True
      ScrollBars      =   2  'Vertical
      TabIndex        =   0
      TabStop         =   0   'False
      Text            =   "frmODBCTrans.frx":0000
      Top             =   240
      Width           =   3375
   End
   Begin VB.Timer timerProcess 
      Enabled         =   0   'False
      Interval        =   200
      Left            =   4080
      Top             =   1080
   End
End
Attribute VB_Name = "frmODBCTrans"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False

Option Explicit



Public m_boolInProcess As Boolean
'Dim mstrIMPORT_FILE_PATH As String
'Dim mobjFSO As FileSystemObject
Public mstrState As String
Dim mdtStatusTime As Date
' Don't get stuck on first file in folder
Dim mlngFileCount As Long
Dim mstrStateSave As String
Dim mstrTransIn As String
Dim mstrTransOut As String
Dim m_dtAutoOpen As Date

Public Event ReplyTransaction(strTransOut As String)
'

Public Sub SendTransaction(strTransIn As String)
    mstrTransIn = strTransIn
End Sub


Private Sub Form_Initialize()

    On Error GoTo ErrorExit
    
    mdtStatusTime = Now
    mstrState = "Start"
    
    DebugPrint "ODBCProcess Init."
    
    Load Me
    
    Exit Sub
ErrorExit:
    MsgBox Err.Description, , "ODBCProcess.Initialize"

End Sub

Private Sub Form_Load()

    DebugPrint "ODBCProcess Load."
    
    Me.timerProcess.Enabled = True

End Sub

Private Sub timerProcess_Timer()

    On Error GoTo ErrorExit
    
    Dim lngCount As Long
    Dim objfile As File
    Dim strMeter As String
    Dim strAccount As String
    
    g_strErrorLocation = "Timer"

    If mstrState <> mstrStateSave Then
        DebugPrint "State change from " & mstrStateSave & " to " & mstrState
        DebugPrint "State change from " & mstrStateSave & " to " & mstrState
        mstrStateSave = mstrState
    End If
    
    ' 2010-08-31, JFW, At Livingston server (only) I'm getting an additional thread on this timer.
    If glngThreadID <> 0 Then
        DebugPrint "ODBCProcess, Timer, invalid thread."
        Exit Sub
    End If
    glngThreadID = App.ThreadID
    
    timerProcess.Enabled = False
'    If mobjFSO Is Nothing Then
'        Set mobjFSO = New FileSystemObject
'    End If
    
    Select Case mstrState
    Case "Start"
        ' Client is warming up or getting connected.
        mstrState = "Connect"
        mdtStatusTime = Now
        
    Case "Connect"
        ' Socket connect to server, give 2 seconds before action
        If DateAdd("s", 5, mdtStatusTime) < Now Then
            mstrState = "Idle"
            mdtStatusTime = Now
        End If
        
    Case "Idle"
        If mstrTransIn <> "" Then
            mstrState = "FileMove"
        Else
            mstrState = "Idle"
        End If
        mlngFileCount = 0
        
    Case "FileMove"
    
        ' g_blnTD = True
'        TransactionBuilder strTrans, 541, "NNNYNNNNN"                               'Open Ship Flags
'        TransactionBuilder strTrans, 542, g_strTDOpenShip

        ' TD master here
        
        g_strErrorLocation = "FileMove"
        If mstrTransIn <> "" Then
            mstrState = "GetData"
        Else
            mstrState = "Idle"
        End If
            
    Case "GetData"
        If mstrTransIn <> "" Then
                    
            ' Incoming trans to globals, otherwise error
            If frmShip.AssignTransToGlobal(mstrTransIn) Then
                If IsNumeric(g_str0) Then
                    If CLng(g_str0) = 20 Or CLng(g_str0) = 23 Then
                        mstrTransIn = GetData(mstrTransIn)
                    Else
                        g_strSaveReply = "0," & Chr(34) & "1" & Right("00" & g_str0, 2) & Chr(34) & "3," & "Trans.Process, Invlaid transaction type, 020 or 023 only, " & g_str0 & "."
                        mstrState = "ShipError"
                        mdtStatusTime = Now
                    End If
                Else
                    g_strSaveReply = "0," & Chr(34) & "1" & Right("00" & g_str0, 2) & Chr(34) & "3," & "Trans.Process, Invlaid transaction type, " & g_str0 & "."
                    mstrState = "ShipError"
                    mdtStatusTime = Now
                End If
            Else
                If g_str0 <> "" Then
                    g_strSaveReply = "0," & Chr(34) & "1" & Right("00" & g_str0, 2) & Chr(34) & "3," & Chr(34) & "Trans.Process, Error parsing transaction." & Chr(34) & "99,"""""
                    mstrState = "ShipError"
                    mdtStatusTime = Now
                Else
                    Err.Raise 12345, "Trans.Process", "Error parsing transaction '" & mstrTransIn & "'"
                End If
            End If
            If mstrTransIn <> "" Then
                If g_str50 = "US" Then
                    mstrState = "TD"
                    mdtStatusTime = Now
                Else
                    mstrState = "Ship"
                    mdtStatusTime = Now
                End If
            Else
                mstrState = "ShipError"
                mdtStatusTime = Now
            End If
        Else
            mstrState = "Idle"
        End If
                
    Case "TD"

        If Format(g_dteTDDate, "yyyymmdd") <> g_str24 Or g_strTDOpenShip = "" Or g_strTDMasterTrkNbr = "" Then
            If g_frmTDAutoOpen Is Nothing Then
                DebugPrint "ODBCTrans.Process, " & "TDAutoOpen"
                mdtStatusTime = Now
        
                Set g_frmTDAutoOpen = New frmTD
                g_frmTDAutoOpen.AutoOpen CDate(g_str24)
                
            ElseIf Now > DateAdd("s", 20, mdtStatusTime) Then
                g_strSaveReply = "0," & Chr(34) & "1" & Right("00" & g_str0, 2) & Chr(34) & "3," & Chr(34) & "Trans.Process, Timeout on server TD master shipment." & Chr(34) & "99,"""""
                mstrState = "ShipError"
                mdtStatusTime = Now
            End If
        
        Else
            
            ' Modify incoming trans, e.g. set TD OpenShipIndex
            g_blnTDChild = True
            
            mstrState = "Ship"
            mdtStatusTime = Now
        End If
                
    Case "Ship"
    
        TransactionStripTag mstrTransIn, "10"
        TransactionStripTag mstrTransIn, "498"
        mstrTransIn = Replace(mstrTransIn, """99,""""", """10,""" & g_str10 & """99,""""")
        mstrTransIn = Replace(mstrTransIn, """99,""""", """498,""" & g_str498 & """99,""""")
            
        
        g_str29 = ""
        'Show Processing in status bar
        frmShip.StatusBar1.Panels(2) = "Processing..."
        Call SendData(mstrTransIn)
        mstrState = "Wait"
        mdtStatusTime = Now
                
    Case "Wait"
        ' Ship button pressed, give the server time seconds to respond.
        If g_str29 <> "" Then
            mdtStatusTime = Now
            mstrState = "Shipped"
        End If
        If DateAdd("s", 20, mdtStatusTime) < Now Then
            g_strSaveReply = "0," & Chr(34) & "1" & Right("00" & g_str0, 2) & Chr(34) & "3," & "Trans.Process, Timeout on reply from server."
            mstrState = "ShipError"
            mdtStatusTime = Now
        End If
        
    Case "ShipError"
        g_strErrorLocation = "ShipError"
        DebugPrint "ODBCProcess, Timer, ShipError, trans " & mstrTransIn
        
        RaiseEvent ReplyTransaction(g_strSaveReply)
        mstrState = "Idle"
        mdtStatusTime = Now
        mstrTransIn = ""
        
    Case "Shipped"
        g_strErrorLocation = "Shipped"
        RaiseEvent ReplyTransaction(g_strSaveReply)
        mstrState = "Idle"
        mlngFileCount = 0
        mstrTransIn = ""
        
    Case Else
        mstrState = "Idle"
        mstrTransIn = ""
        
    End Select
    
ExitHere:
    glngThreadID = 0
    timerProcess.Enabled = True
    Exit Sub
    
ErrorExit:
    ' MsgBox mstrIMPORT_FILE_PATH
    MsgBox "Error in Process Timer, status " & mstrState & ", step " & g_strErrorLocation & ", " & Err.Description, , "ODBCProcess.Timer"
    mstrState = "Idle"
    GoTo ExitHere
    
End Sub

